﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode
{
    class P0009 : IProblem
    {
        public void Run()
        {

            var list = new List<int>
            {
                0,
                11,
                1,
                -1,
                151125203,
                5646,
                656,
                1111111,
            };
            foreach (var v in list)
            {
                var startTime = DateTime.Now;
                var result = IsPalindrome(v);
                var endTime = DateTime.Now;

                Console.WriteLine(string.Format("input:{0} output:{1} useTime:{2}", v, result, (endTime - startTime).TotalMilliseconds));
            }
        }
        public bool IsPalindrome(int x)
        {
            if (x < 0)
                return false;

            if (x == 0)
                return true;

            int length = 0;
            int t = x;
            while (t > 0)
            {
                t /= 10;
                length++;
            }
            for (int i = 0; i < length; i++)
            {
                if (GetNumAtIndex(x, i) != GetNumAtIndex(x, length - 1 - i))
                {
                    return false;
                }
            }
            return true;
        }

        private int GetNumAtIndex(int value, int index)
        {
            var t = value % (int)Math.Pow(10, index + 1);
            return t / (int)Math.Pow(10, index);
        }
    }
}
